package com.wish.house.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.common.core.utils.StringUtils;
import com.wish.common.core.web.controller.BaseController;
import com.wish.common.core.web.domain.AjaxResult;
import com.wish.common.core.web.page.TableDataInfo;
import com.wish.common.log.annotation.Log;
import com.wish.common.log.enums.BusinessType;
import com.wish.common.security.annotation.RequiresPermissions;
import com.wish.house.entity.House;
import com.wish.house.service.IHouseService;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 房源表 前端控制器
 * </p>
 *
 * @author lyk
 * @since 2022-11-18
 */
@RestController
@RequestMapping("/house")
public class HouseController extends BaseController {

    @Autowired
    IHouseService houseService;


    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;


    @GetMapping("/bulkIndex")
    public void bulkIndex(){
        List<House> list = houseService.list();
        List<IndexQuery> indexQueryList = new ArrayList<>();
        for (House house : list) {
            IndexQuery indexQuery = new IndexQueryBuilder()
                    .withId(house.getId().toString())
                    .withObject(house)
                    .build();
            indexQueryList.add(indexQuery);
        }
        elasticsearchRestTemplate.bulkIndex(indexQueryList,House.class);
    }

    @GetMapping("/queryForList")
    public SearchHits<House> queryForList(){
        MatchAllQueryBuilder allQuery = QueryBuilders.matchAllQuery();
        NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(allQuery);
        SearchHits<House> search = elasticsearchRestTemplate.search(nativeSearchQuery, House.class);
        for (SearchHit<House> houseSearchHit : search) {
            System.out.println(houseSearchHit);
        }
        return search;
    }




    @GetMapping("/list")
    //@RequiresPermissions("house:house:list")
    public TableDataInfo list(House house) {
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(house.getHouseName())) {
            queryWrapper.like("house_name",house.getHouseName());
        }
        if (StringUtils.hasText(house.getAddress())) {
            queryWrapper.like("address",house.getAddress());
        }
        if (house.getRent()!=null) {
            queryWrapper.ge("rent",house.getRent());
        }
        startPage();
        List<House> list = houseService.list(queryWrapper);
        return getDataTable(list);
    }

    @GetMapping("/getHouseList")
    public List<House> getHouseList() {
        return houseService.list();
    }

    @GetMapping("/getHouseById")
    public House getHouseById(Integer id) {
        return houseService.getById(id);
    }

    /**
     * 新增房源
     */
    @RequiresPermissions("house:house:add")
    @Log(title = "房源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody House house)
    {
        boolean b = houseService.save(house);
        if (b) {
            House house1 = houseService.getById(house.getId());
            IndexQuery indexQuery = new IndexQueryBuilder()
                    .withId(house1.getId().toString())
                    .withObject(house1)
                    .build();
            elasticsearchRestTemplate.save(house1,indexQuery);
        }
        return toAjax(b);
    }

    /**
     * 删除房源
     */
    @RequiresPermissions("house:house:remove")
    @Log(title = "房源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(houseService.removeBatchByIds(Arrays.asList(ids)));
    }

}
